package io.renren.modules.canteens.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.exception.RRException;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtil;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.canteens.dto.BatchOrdertemDTO;
import io.renren.modules.canteens.dto.FoodDTO;
import io.renren.modules.canteens.entity.BatchOrdertemEntity;
import io.renren.modules.canteens.excel.BatchOrdertemExcel;
import io.renren.modules.canteens.service.BatchOrdertemService;
import io.renren.modules.canteens.service.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 批量采购明临时表 
 *
* @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2020-01-06
 */
@RestController
@RequestMapping("canteens/batchordertem")
@Api(tags="批量采购表")
public class BatchOrdertemController {
    @Autowired
    private BatchOrdertemService batchOrdertemService;
    @Autowired
    private FoodService foodService;
    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody BatchOrdertemDTO dto){
        Result result = new Result();

        FoodDTO foodDTO = foodService.getName(dto.getFoodName());
        if(null ==foodDTO){
            result.setMsg(dto.getFoodName()+"菜品不存在");
            result.setCode(5001);
            return result;
        }else {
            dto.setUnit(foodDTO.getUnit());
            if(null==dto.getPurchaseNum()|| null ==dto.getUnitPrice()){
                result.setMsg(dto.getFoodName()+"数量或价格不能为空");
                result.setCode(5001);
                return result;
            }
            dto.setMoney(dto.getPurchaseNum()*dto.getUnitPrice());
            batchOrdertemService.save(dto);
            result.setMsg(dto.getFoodName()+"保存成功");
            result.setCode(0);
            return result;
        }
    }

    @PostMapping("update")
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody BatchOrdertemDTO dto){
        Result result = new Result();
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        FoodDTO foodDTO = foodService.getName(dto.getFoodName());
        if(null ==foodDTO){
            result.setMsg(dto.getFoodName()+"菜品不存在");
            result.setCode(5001);
            return result;
        }else {
            if (null == dto.getPurchaseNum() || null == dto.getUnitPrice()) {
                result.setMsg(dto.getFoodName() + "数量或价格不能为空");
                result.setCode(5001);
                return result;
            }

            dto.setMoney(dto.getPurchaseNum() * dto.getUnitPrice());
            batchOrdertemService.update(dto);
            result.setMsg(dto.getFoodName() + "修改成功");
            result.setCode(0);
            return result;
        }
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody BatchOrdertemDTO dto){

        batchOrdertemService.delete1(dto.getBatchOrdertemId());

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("canteens:batchordertem:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<BatchOrdertemDTO> list = batchOrdertemService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, BatchOrdertemExcel.class);
    }
    /**
     * @method: Post
     * @desc: 导入excel数据
     */
    @PostMapping("/excelImport")
    @ApiOperation("导入数据")
    public Result exportdatas(@RequestParam("file") MultipartFile file ) throws Exception {
        if (file.isEmpty()) {
            throw new RRException("上传文件不能为空");
        }
        //上传文件
        List<BatchOrdertemExcel>  list= ExcelUtil.importExcel(file,0, 1, BatchOrdertemExcel.class);
        if(list.size()<=0){
            throw new RRException("上传文件格式不正确");
        }
        Result result = batchOrdertemService.savemportAmount(list);
        return result;
    }
    @GetMapping("/getBatchOrdermx")
    @ApiOperation("供应商订单详情信息")
    public Result<List<BatchOrdertemDTO>> getBatchOrdermx(){
        List<BatchOrdertemDTO> list = batchOrdertemService.getBatchOrdermx();
        return new Result<List<BatchOrdertemDTO>>().ok(list);
    }
    @GetMapping("/getTotalMoney")
    @ApiOperation("获取金额")
    public Result<BatchOrdertemDTO> getTotalMoney(){
        BatchOrdertemDTO dto = batchOrdertemService.getTotalMoney();
        return new Result<BatchOrdertemDTO>().ok(dto);
    }

}